package com.jinbei.restController;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jinbei.common.Enum.ApproverEnum;
import com.jinbei.common.Enum.ConstantsEnum;
import com.jinbei.common.annotation.AuthorAnnotation;
import com.jinbei.common.base.ResponseBase;
import com.jinbei.common.base.ResponseBaseEntity;
import com.jinbei.common.contants.Contants;
import com.jinbei.common.util.TokenUtil;
import com.jinbei.domain.PayOrder;
import com.jinbei.domain.User;
import com.jinbei.service.PayOrderService;
import com.jinbei.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author zflu
 * 运营元宝充值工作流restful接口
 * Created by lucky on 2018/9/17.
 */
@RestController
@RequestMapping("/Order")
public class PayOrderController {

    private final Logger logger = LoggerFactory.getLogger(PayOrderController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private PayOrderService payOrderService;

    /**
     * @author zflu
     * @Description   运营人员创建元宝充值订单
     * @date 2018/9/17 15:40
     * @param request
     * @param payOrder
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/insertPayOrder" , logs = "元宝充值订单创建")
    @PostMapping("/insertPayOrder")
    public ResponseBase insertPayOrder(HttpServletRequest request, @RequestBody PayOrder payOrder){
        try {
            JSONObject token = TokenUtil.getToken(request);
            if(token.isEmpty()){
                return ResponseBaseEntity.setResultError(ConstantsEnum.ERROR_501);
            }
            String loginName = (String) token.get(Contants.LOGIN_NAME);
            User queryUser = userService.queryUserByLoginName(loginName);
            if(queryUser != null){
                payOrder.setUserId(queryUser.getUserId());
                int result = payOrderService.insertPayOrder(payOrder);
                if(result > 0){
                    return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_TRUE);
                }else{
                    return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_FALSE);
                }
            }else{
                return ResponseBaseEntity.setResultError(ConstantsEnum.ERROR_501);
            }
        }catch (Exception e){
            logger.error("运营人员创建元宝充值订单失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description   用户元宝充值订单审批
     * @date 2018/9/17 16:38
     * @param request
     * @param payOrder
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/approverPayOrder" ,logs = "用户元宝充值订单审批")
    @PutMapping("/approverPayOrder")
    public ResponseBase approverPayOrder(HttpServletRequest request, @RequestBody PayOrder payOrder){
        try {
            JSONObject token = TokenUtil.getToken(request);
            if(token.isEmpty()){
                return ResponseBaseEntity.setResultError(ConstantsEnum.ERROR_501);
            }
            String loginName = (String) token.get(Contants.LOGIN_NAME);
            User queryUser = userService.queryUserByLoginName(loginName);
            if(queryUser != null) {
                payOrder.setApproverId(queryUser.getUserId());
                int result = payOrderService.approverPayOrder(payOrder, request);
                if (result > 0) {
                    return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_TRUE);
                } else {
                    return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_FALSE);
                }
            }else {
                return ResponseBaseEntity.setResultError(ConstantsEnum.ERROR_501);
            }
        }catch (Exception e){
            logger.error("用户元宝充值订单审批失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description  用户元宝充值订单删除
     * @date 2018/9/17 17:00
     * @param id
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/deletePayOrder" , logs = "用户元宝充值订单删除")
    @DeleteMapping("/deletePayOrder")
    public ResponseBase deletePayOrder(@RequestParam("id") Integer id){
        try {
            int result = payOrderService.deletePayOrder(id);
            if(result > 0){
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_TRUE);
            }else{
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_FALSE);
            }
        }catch (Exception e){
            logger.error("用户元宝充值订单删除失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description    用户元宝充值订单详情
     * @date 2018/9/17 17:38
     * @param id
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/queryPayOrderById" , logs = "用户元宝充值订单详情")
    @GetMapping("/queryPayOrderById")
    public ResponseBase queryPayOrderById(@RequestParam("id") Integer id){
        try {
            PayOrder result = payOrderService.queryPayOrderById(id);
            return ResponseBaseEntity.setResultSuccess(result);
        }catch (Exception e){
            logger.error("用户元宝充值订单删除失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }


    /**
     * @author zflu
     * @Description   审批状态查询
     * @date 2018/9/17 17:37
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/queryPayOrderStatus" , logs = "审批状态查询")
    @GetMapping("/queryPayOrderStatus")
    public ResponseBase queryPayOrderStatus(){
        try {
            List<Map<String, String>> maps = ApproverEnum.queryEnumList();
            return ResponseBaseEntity.setResultSuccess(maps);
        }catch (Exception e){
            logger.error("审批状态查询失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description  元宝充值订单列表
     * @date 2018/9/17 17:56
     * @param pageNum
     * @param pageSize
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/queryPayOrderPage" , logs = "元宝充值审批分页列表")
    @GetMapping("/queryPayOrderPage")
    public ResponseBase queryPayOrderPage(@RequestParam("pageNum") int pageNum,
                                          @RequestParam("pageSize") int pageSize ){
        try {
            PageInfo<PayOrder> payOrders = payOrderService.queryPayOrderPage(pageNum, pageSize);
            return ResponseBaseEntity.setResultSuccess(payOrders);
        }catch (Exception e){
            logger.error("元宝充值审批分页列表查询失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }
}
